/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processor;

import java.awt.Component;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author DYNG09
 */
public class MyFile {

    private Component component;
    private String content;
    private File fileChoosed = null;

    /**
     * Constructor
     * @param com a parent component
     */
    public MyFile(Component com) {
        this.component = com;
    }

    /**
     * save text chains into a text file with extension .myfile
     */
    public void saveEditedFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new MyFileFilter());
        int r = fileChooser.showSaveDialog(component);
        if (JFileChooser.APPROVE_OPTION == r) {
            BufferedWriter bw = null;
            try {
                fileChoosed = fileChooser.getSelectedFile();
                bw = new BufferedWriter(new FileWriter(fileChoosed.getAbsolutePath() + ".myfile"));
                
                bw.write(content, 0, content.length());
                
            } catch (IOException ex) {
                Logger.getLogger(MyFile.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    bw.close();
                } catch (IOException ex) {
                    Logger.getLogger(MyFile.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    /**
     * load contains of a text file with extension .myfile to edit area
     */
    public void loadFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new MyFileFilter());
        int r = fileChooser.showOpenDialog(component);
        if (JFileChooser.APPROVE_OPTION == r) {
            //read content of file
            BufferedReader br = null;
            try {
                fileChoosed = fileChooser.getSelectedFile();
                br = new BufferedReader(new FileReader(getFileChoosed().getAbsolutePath()));
                setContent("");
                while (true) {
                    String str = br.readLine();
                    if (str == null) {
                        break;
                    }
                    setContent(getContent() + str + "\n");
                }
            } catch (IOException ex) {
                Logger.getLogger(MyFile.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    br.close();
                } catch (IOException ex) {
                    Logger.getLogger(MyFile.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * @return the fileChoosed
     */
    public File getFileChoosed() {
        return fileChoosed;
    }
    
    /**
     * class for filtering files with extension .myfile
     */
    private class MyFileFilter extends FileFilter{

        @Override
        public boolean accept(File f) {
            if(f.isFile()){
                String name = f.getName();
                if(name.endsWith(".myfile"))
                    return true;
            }
            
            return false;
        }

        @Override
        public String getDescription() {
            return "My File";
        }
    }
}
